package com.link.base.base.message.controller;

import com.link.base.base.message.model.MessageReceiver;
import com.link.base.base.message.service.MessageReceiverService;
import com.link.base.base.user.model.User;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息发布-接收对象-消息发布维护接收用户 
 *
 * @author ouyangzhenqiang
 * @date 2019/07/25 13:54
 */
@Controller
@RequestMapping("/link/messageReceiver")
public class MessageReceiverController extends BasicController<MessageReceiver> {

    @Resource
    private MessageReceiverService messageReceiverService;

    @Override
    public BasicService<MessageReceiver> getBasicService() throws Exception {
        return messageReceiverService;
    }

    /**
     * 查询未维护到消息接收人中的用户
     *
     * @author ouyangzhenqiang
     * @date 2019/07/25 14:00
     */
    @PostMapping(value = "/queryUnSelectUserListPage", produces = "application/json")
    @ResponseBody
    public Map<String, Object> queryUnSelectUserListPage(
            @JsonParam MessageReceiver record, @JsonParam QueryParams qps,
            HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(4);
        try {
            MessageReceiver t = BasicModel.transformClass(MessageReceiver.class, qps);
            t.setMsgId(record.getMsgId());
            List<User> list = messageReceiverService.queryUnSelectUserListPage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount(t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<User>());
            result.put("result", e.getMessage());
            if (e instanceof BasicServiceException) {
                result.put("detailMessage", ((BasicServiceException) e).getDetailMessage());
            }
        }
        return result;
    }

}
